
//var commonCityData = require('../../utils/city.js')
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataArr: [],
    selectDate: 0,
    CurrentPage: 1,
    goodsResult: [],
    loadingHidden: false, // loading
    mobile: '',
    selectdates: "",//选择日期
    selectdays: "",//星期
    selProvince: "广东省",
    selCity: "深圳市",
    selDistrict: "福田区"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    var that = this;
    that.fun_submit();
    that.searchGoods();
    that.about();
  },
  //获取地址
  initShippingAddress: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/user/GetDefaultShipAddress',
      data: {
        uid: app.globalData.userInfo.Uid
      },
      success: (res) => {
        if (res.data.Code == 0) {
          if (res.data.Data == null || res.data.Data == ""){
            that.bindSave();
          }
         
        }
      }
    })
  },
  //默认收货地址
  bindSave: function () {
    var that = this;
    var consignee = '挂号';
    var address = '玉田大厦';
    var mobile = '18888888888';
    var zipcode = '130000';
    var apiAddid = 0;
    wx.request({
      url: app.globalData.host + '/api/User/AddShipAddress',
      data: {
        uid: app.globalData.userInfo.Uid,
        said: apiAddid,
        regionid: 2143,
        consignee: consignee,
        address: address,
        mobile: mobile,
        zipcode: zipcode,
        isDefault: 1
      },
      success: function (res) {
        if (res.data.Code != 0) {
          // 登录错误 
          wx.hideLoading();
          wx.showModal({
            title: '失败',
            content: res.data.Message,
            showCancel: false
          })
          return;
        }
      }
    })
  },
  //清空购物车
  clearShopCar: function (pid, name, shopprice) {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Cart/Clear',
      data: {
        uid: app.globalData.userInfo.Uid,
      },
      success: function (res) {
        if (res.data.Code == 0) {
          that.addShopCar(pid, name, shopprice);//加入购物车
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  //加入购物车
  addShopCar: function (pid, name, shopprice) {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/cart/AddProduct',
      data: {
        uid: app.globalData.userInfo.Uid,
        pid: pid,
        buyCount: 1
      },
      success: function (res) {
        if (res.data.Code == 0) {
          console.log("加入购物车")
          that.setData({
            shopNum: res.data.Data//购物车数量
          });
          wx.navigateTo({
            url: '/pages/yuyue/index?doctor=' + name + "&pid=" + pid + "&shopprice=" + shopprice + "&dates=" + that.data.selectdates + "&weeks=" + that.data.selectdays,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false,
            success:function(){
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }
      }
    })
  },
  toyuyue: function (e) {
    var that = this;
    var name = e.currentTarget.dataset.name
    var pid = e.currentTarget.dataset.pid
    var shopprice = e.currentTarget.dataset.shopprice
    that.clearShopCar(pid, name, shopprice);
  },
  call1: function () {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.mobile
    })
  },
  about: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/store/GetInfo?id=' + app.globalData.storeId,
      success: function (res) {
        var Mobile = res.data.Data.Mobile
        that.setData({
          mobile: Mobile
        })
      }
    })
  },
  //选择时间
  selectDay: function (e) {
    var select = e.currentTarget.dataset.select
    var selectdates = e.currentTarget.dataset.selectdates
    var selectdays = e.currentTarget.dataset.selectdays
    this.setData({
      selectDate: select,
      selectdates: selectdates,
      selectdays: selectdays
    })
  },
  //获取商品
  searchGoods: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/store/Search',
      data: {
        storeid: app.globalData.storeId,
        cateId: 0,
        word: '',
        page: that.data.CurrentPage,
        pageSize: 8,
        priceStart: 0,
        priceEnd: 0
      },
      success: function (res) {
        var newbs = [];
        var rawData = res.data.Data;
        var products = rawData.ProductList;
        for (var i = 0; i < products.length; i++) {
          var d = products[i];
          d.ShowImg = app.globalData.image800_800host + d.ShowImg;
          newbs.push(d);
        }
        if (that.data.CurrentPage != 1) {
          var preGoods = that.data.goodsResult
          newbs = preGoods.concat(newbs);
        }
        that.setData({
          goodsResult: newbs,
          loadingMoreHidden: rawData.PageModel.HasNextPage
        });
      }
    })

  },
  loadMore: function (e) {
    var that = this;
    if (!that.data.loadingMoreHidden) return;
    var currPage = that.data.CurrentPage;
    that.setData({
      CurrentPage: currPage + 1
    })
    that.searchGoods();
  },
  //日期选择
  fun_submit: function () {
    var that = this;
    var dataArr = []
    var show_day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
    //第一天  
    var date1 = new Date();
    var day = date1.getDay();
    that.setData({
      selectdates: (date1.getMonth() + 1) + "月" + date1.getDate() + "日",
      selectdays: show_day[day]
    })
    //第二天  
    var date2 = new Date(date1);
    date2.setDate(date1.getDate() + 1);
    var day2 = date2.getDay();
    //第三天  
    var date3 = new Date(date1);
    date3.setDate(date1.getDate() + 2);
    var day3 = date3.getDay();
    //第四天  
    var date4 = new Date(date1);
    date4.setDate(date1.getDate() + 3);
    var day4 = date4.getDay();

    //第五天  
    var date5 = new Date(date1);
    date5.setDate(date1.getDate() + 4);
    var day5 = date5.getDay();

    //第六天  
    var date6 = new Date(date1);
    date6.setDate(date1.getDate() + 5);
    var day6 = date6.getDay();

    //第七天  
    var date7 = new Date(date1);
    date7.setDate(date1.getDate() + 6);
    var day7 = date7.getDay();

    dataArr.push(
      {
        dates: (date1.getMonth() + 1) + "月" + date1.getDate() + "日",
        days: show_day[day]
      },
      {
        dates: (date2.getMonth() + 1) + "月" + date2.getDate() + "日",
        days: show_day[day2]
      },
      {
        dates: (date3.getMonth() + 1) + "月" + date3.getDate() + "日",
        days: show_day[day3]
      },
      {
        dates: (date4.getMonth() + 1) + "月" + date4.getDate() + "日",
        days: show_day[day4]
      },
      {
        dates: (date5.getMonth() + 1) + "月" + date5.getDate() + "日",
        days: show_day[day5]
      },
      {
        dates: (date6.getMonth() + 1) + "月" + date6.getDate() + "日",
        days: show_day[day6]
      },
      {
        dates: (date7.getMonth() + 1) + "月" + date7.getDate() + "日",
        days: show_day[day7]
      }
    )
    that.setData({
      dataArr: dataArr
    })
  },



  initCityData: function (level, obj) {
    if (level == 1) {
      var pinkArray = [];
      for (var i = 0; i < commonCityData.cityData.length; i++) {
        pinkArray.push(commonCityData.cityData[i].Name);
      }
      this.setData({
        provinces: pinkArray
      });
    } else if (level == 2) {
      var pinkArray = [];
      var dataArray = obj.CityList
      for (var i = 0; i < dataArray.length; i++) {
        pinkArray.push(dataArray[i].Name);
      }
      this.setData({
        citys: pinkArray
      });
    } else if (level == 3) {
      var pinkArray = [];
      var dataArray = obj.DistrictList
      for (var i = 0; i < dataArray.length; i++) {
        pinkArray.push(dataArray[i].Name);
      }
      this.setData({
        districts: pinkArray
      });
    }

  },
  bindPickerProvinceChange: function (event) {
    var selIterm = commonCityData.cityData[event.detail.value];
    this.setData({
      selProvince: selIterm.Name,
      selProvinceIndex: event.detail.value,
      selCity: '请选择',
      selDistrict: '请选择'
    })
    this.initCityData(2, selIterm)
  },
  bindPickerCityChange: function (event) {
    var selIterm = commonCityData.cityData[this.data.selProvinceIndex].CityList[event.detail.value];
    this.setData({
      selCity: selIterm.Name,
      selCityIndex: event.detail.value,
      selDistrict: '请选择'
    })
    this.initCityData(3, selIterm)
  },
  bindPickerChange: function (event) {
    var selIterm = commonCityData.cityData[this.data.selProvinceIndex].CityList[this.data.selCityIndex].DistrictList[event.detail.value];
    this.setData({
      selDistrict: selIterm.Name,
      selDistrictIndex: event.detail.value
    })
  },

  selectCity: function () {

  },
  deleteAddress: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/user/DelShipAddress',
            data: {
              uid: app.globalData.userInfo.Uid,
              said: id
            },
            success: (res) => {
              wx.navigateBack({})
            }
          })
        } else if (res.cancel) {
        
        }
      }
    })
  },
  toDetailsTap: function (e) {
    var pid = e.currentTarget.dataset.pid
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + pid
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initShippingAddress()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})